﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Web;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Web.Navigation;

namespace blackhouse.Aparts.Web.Modules {
	public partial class SearchStateControl : UserControl {

		#region Fields

		private SearchState queryState = SearchState.GetFromContext();

		#endregion

		#region Event Handlers

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			if (!this.queryState.IsSet) {
				this.Visible = false;
				return;
			}

			this.ltlArrivalDate.Text = this.queryState.ArrivalDate.ToString(this.GetFormatArrivalDate());
			this.ltlDepartureDate.Text = this.queryState.DepartureDate.ToString("d MMMM yyyy");
			this.ltlPersonCount.Text = this.queryState.PeopleCount + " ";
			if (this.queryState.PeopleCount == 1)
				this.ltlPersonCount.Text += "osoby";
			else
				this.ltlPersonCount.Text += "osób";

			BlackApart.Web.DisplayApartmentTypeState state = BlackApart.Web.DisplayApartmentTypeState.GetCurrentState();
            this.lnkToSearchPanel.NavigateUrl = NavigationProvider.Instance().GetUrl(this.queryState);

			if (state.ApartmentType != null) {
				this.lnkReservationPopup.Apartment = state.ApartmentType;

				this.lnkToReservation.NavigateUrl = UrlUtils.LocalizeUrl(string.Format("/pages/progressreservation.aspx?search={0}&group={1}", this.queryState, state.ApartmentType.Id));
				this.lnkToReservation.ToolTip = "Zarezerwuj apartament - " + state.ApartmentType.Name;
			}
			else {
				this.pnlReservation.Visible = false;
			}
		}

		#endregion

		#region Methods

		private string GetFormatArrivalDate() {
			if (queryState.ArrivalDate.Year != queryState.DepartureDate.Year)
				return "d MMMM yyyy";
			else {
				string format = "d ";
				if (queryState.ArrivalDate.Month != queryState.DepartureDate.Month)
					format += "MMMM";
				return format;
			}

		}

		#endregion

	}
}